<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Log;

/**
 * 示例接口
 */
class Demo extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['querystudentinfo','qidno','test', 'test1'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['test2'];


    /**
     * 学生导入重复值
     *
     * @param string $idno
     */
    public function qidno()
    {
        $idno = $this->request->request('idno');
        Log::info("验证身份证重复值收到请求：".$idno);
        if($idno==null){
            $this->error('返回成功', ['msg' => '身份证号码不能为空']);
        }else{
            $list= Db::table('fa_studentinfo')->where('idno',$idno)->select();
            if($list==null){
                $this->error('返回成功', ['msg' => '没有该学生的缴费信息']);
            }else{
                $count=count($list);
                $idstr="id分别是";
                foreach ($list as $k => $v) {
                    $idstr=$idstr.$v['id'].'缴费状态是：';
                    if(strcmp($v['status'],'0' )==0){
                        $idstr=$idstr."未交费；";
                    }else if(strcmp($v['status'],'1' )==0){
                        $idstr=$idstr."已缴学费住宿费；";
                    }else if(strcmp($v['status'],'2' )==0){
                        $idstr=$idstr."已缴代收费；";
                    }else if(strcmp($v['status'],'3' )==0){
                        $idstr=$idstr."已缴齐；";
                    }
                }
                $this->success('返回成功', ['listcount' =>$count,'listidstr' =>$idstr]);

            }

        }

    }

    /**
     * 学生费用查询
     *
     * @param string $idno
     */
    public function querystudentinfo()
    {
        $idno = $this->request->request('idno');
        Log::info("收到请求：".$idno);
        if($idno==null){
            $this->error('返回成功', ['msg' => '身份证号码不能为空']);
        }else{
            $wheresta = [];
            $wheresta['status'] = ['in', ['0','1','2']];
            $list= Db::table('fa_studentinfo')->where('idno',$idno)->where($wheresta)->order('id desc')->find();
            if($list==null){
                $list= Db::table('fa_studentinfo')->where('idno',$idno)->order('id desc')->select();
                if($list==null){
                    $this->error('返回成功', ['msg' => '没有该学生的缴费信息']);
                }else{
                    $count=count($list);
                    $username =$list[0]['username'];
                    $sex =$list[0]['sex'];
                    $idno =$list[0]['idno'];
                    $tel =$list[0]['tel'];
                    $grade =$list[0]['grade'];
                    $year=$list[0]['year'];
                    $classname =$list[0]['classname'];
                    $status =$list[0]['status'];
                    $pay1 =0;
                    $pay2 =0;
                    $pay3 =0;
                    $pay4=0;
                    $pay6 =0;
                    $pay7 =0;
                    $pay8 =0;
                    $pay9 =0;
                    $pay5 =0;

                    foreach ($list as $k => $v) {
                        $pay1 =$pay1+$v['pay1'];
                        $pay2 =$pay2+$v['pay2'];
                        $pay3 =$pay3+$v['pay3'];
                        $pay4= $pay4+$v['pay4'];
                        $pay6 =$pay6+$v['pay6'];
                        $pay7 =$pay7+$v['pay7'];
                        $pay8 =$pay8+$v['pay8'];
                        $pay9 =$pay9+$v['pay9'];
                        $pay5 =$pay5+$v['pay5'];
                    }
                    $this->success('返回成功', ['username' =>$username,'sex' =>$sex,'idno' =>$idno,'grade' =>$grade,'year' =>$year,'classname' =>$classname,'tel' =>$tel,'status' =>$status,'pay1' =>$pay1,'pay2' =>$pay2,'pay3' =>$pay3,'pay4' =>$pay4,'pay5' =>$pay5,'pay6' =>$pay6,'pay7' =>$pay7,'pay8' =>$pay8,'pay9' =>$pay9]);


                }
            }
            //$list= Db::table('fa_studentinfo')->where('idno',$idno)->order('id desc')->find();
            if($list==null){
                $this->error('返回成功', ['msg' => '没有该学生的缴费信息']);
            }else{
                $username =$list['username'];
                $sex =$list['sex'];
                $idno =$list['idno'];
                $tel =$list['tel'];
                $grade =$list['grade'];
                $year=$list['year'];
                $classname =$list['classname'];
                $status =$list['status'];
                $pay1 =$list['pay1'];
                $pay2 =$list['pay2'];
                $pay3 =$list['pay3'];
                $pay4=$list['pay4'];
                $pay6 =$list['pay6'];
                $pay7 =$list['pay7'];
                $pay8 =$list['pay8'];
                $pay9 =$list['pay9'];
                $pay5 =$list['pay5'];

                $this->success('返回成功', ['username' =>$username,'sex' =>$sex,'idno' =>$idno,'grade' =>$grade,'year' =>$year,'classname' =>$classname,'tel' =>$tel,'status' =>$status,'pay1' =>$pay1,'pay2' =>$pay2,'pay3' =>$pay3,'pay4' =>$pay4,'pay5' =>$pay5,'pay6' =>$pay6,'pay7' =>$pay7,'pay8' =>$pay8,'pay9' =>$pay9]);

            }

        }

    }



    /**
     * 测试方法
     *
     * @ApiTitle    (测试名称)
     * @ApiSummary  (测试描述信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/demo/test/id/{id}/name/{name})
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="会员ID")
     * @ApiParams   (name="name", type="string", required=true, description="用户名")
     * @ApiParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
         'code':'1',
         'msg':'返回成功'
        })
     */
    public function test()
    {
        $this->success('返回成功', $this->request->param());
    }

    /**
     * 无需登录的接口
     *
     */
    public function test1()
    {
        $paylist= Db::table('fa_bill')->field('transDate ,sum(cast(totalAmt as decimal(20,2))) as money')->where('epayCode','JF-EPAY2021082332174')->group('transDate')->order('transDate')->select();
        $this->success('返回成功', ['todayall'=>json_encode($paylist),'action' => 'test1']);
    }

    /**
     * 需要登录的接口
     *
     */
    public function test2()
    {
        $this->success('返回成功', ['action' => 'test2']);
    }

    /**
     * 需要登录且需要验证有相应组的权限
     *
     */
    public function test3()
    {
        $this->success('返回成功', ['action' => 'test3']);
    }

}
